const express = require('express')
const router = express.Router()
const RunSQL = require('../src/db/utils')
const {
    queryAll,
    queryAll1,
    queryPageAll,
    queryPageAll1,
} = require('../src/db/downIMG/index') // sql
const { getWallpaper, getPortrait } = require('../src/downloadIMG/index')



router.get('/getWallpaperAll', (req, res) => { // 查询全部微信壁纸
    RunSQL(queryAll).then(result => {
        res.send({
            code: 200,
            data: result,
            msg: '查询成功'
        })
    }).catch(err => {
        res.send({
            code: 500,
            data: null,
            msg: '查询错误'
        })
    })
})

router.all('/getWallpaperPage', (req, res) => { // 分页查询微信壁纸数据
    const body = req.body
    const query = req.query
    const isData = data => { // 查询函数
        const pageNum = data.pageNum
        const pageSize = data.pageSize
        const params = [(parseInt(pageNum) - 1) * parseInt(pageSize), parseInt(pageSize)]
        RunSQL(queryPageAll, params).then(result => {
            if(result) {
                res.send({
                    code: 200,
                    data: result,
                    msg: '查询成功'
                })
            }else res.send({
                code: 201,
                data: null,
                msg: '查询失败'
            })
        })
    }
    if(body.pageNum) {
        isData(body)
    }else if(query.pageNum) {
        isData(query)
    }else res.send({
        code: 500,
        data: null,
        msg: '查询错误'
    })

})

router.post('/addWallpaper', (req, res) => { // 添加微信壁纸
    const { url } = req.body
    if(url) {
        getWallpaper(url).then(result => {
            if(result) {
                res.send({
                    code: 200,
                    data: 'ok',
                    msg: '正在下载中...'
                })
            } else res.send({
                code: 201,
                data: 'ok',
                msg: 'url不存在图片信息'
            })
        })
    } else res.send({
        code: 500,
        data: null,
        msg: '非法访问'
    })
})

router.get('/getPortraitAll', (req, res) => { // 查询全部微信头像
    RunSQL(queryAll1).then(result => {
        res.send({
            code: 200,
            data: result,
        })
    })
})

router.all('/getPortraitPage', (req, res) => { // 分页查询微信头像数据
    const body = req.body
    const query = req.query
    const isData = data => { // 查询函数
        const pageNum = data.pageNum
        const pageSize = data.pageSize
        const params = [(parseInt(pageNum) - 1) * parseInt(pageSize), parseInt(pageSize)]
        RunSQL(queryPageAll1, params).then(result => {
            if(result) {
                res.send({
                    code: 200,
                    data: result,
                    msg: '查询成功'
                })
            }else res.send({
                code: 201,
                data: null,
                msg: '查询失败'
            })
        })
    }
    if(body.pageNum) {
        isData(body)
    }else if(query.pageNum) {
        isData(query)
    }else res.send({
        code: 500,
        data: null,
        msg: '查询错误'
    })

})

router.post('/addPortrait', (req, res) => { // 添加微信头像
    const { url } = req.body
    if(url) {
        getPortrait(url).then(result => {
            if(result) {
                res.send({
                    code: 200,
                    data: 'ok',
                    msg: '正在下载中...'
                })
            } else res.send({
                code: 201,
                data: 'ok',
                msg: 'url不存在图片信息'
            })
        })
    } else res.send({
        code: 500,
        data: null,
        msg: '非法访问'
    })
})



router.get('*', (req, res) => {
    const state = req.path.includes('/static')
    if(!state) res.redirect('../static/404.html')
})
module.exports = router